﻿// Motorola W375 Manager
// Copyright (C) 2008-2009 Gorka Elexgaray
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using System.Globalization;
using System.Text;

namespace MotorolaManager.Common
{
	/// <summary>
	/// Utilidades de manipulación de strings
	/// </summary>
	public static class StringUtils
	{
		/// <summary>
		/// Método que elimina los caracteres diacríticos
		/// de un string
		/// </summary>
		/// <remarks>
		/// Tomado de
		/// http://blogs.msdn.com/michkap/archive/2007/05/14/2629747.aspx
		/// </remarks>
		/// <param name="stIn">Cadena de entrada</param>
		/// <returns>Cadena de texto con los diacríticos eliminados</returns>
		public static string RemoveDiacritics(string stIn) {
			string stFormD = stIn.Normalize(NormalizationForm.FormD);
			StringBuilder sb = new StringBuilder();

			for(int ich = 0; ich < stFormD.Length; ich++) {
				UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
				if(uc != UnicodeCategory.NonSpacingMark) {
					sb.Append(stFormD[ich]);
				}
			}

			return(sb.ToString().Normalize(NormalizationForm.FormC));
		}

	}
}
